package com.example.zyghfeedback;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MyFeedbackModel> mydata;


    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    public MyAdapter(List<MyFeedbackModel> myFeedback) {
        this.mydata = myFeedback;
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // 绑定数据
        MyFeedbackModel myFeedbackModel = mydata.get(position);
        holder.tv_date.setText(getUserDate(myFeedbackModel.getFeedbackTime()));
        holder.tv_reply.setText(myFeedbackModel.getRead());
        holder.tv_who.setText(myFeedbackModel.getSource());
        holder.tv_text.setText(myFeedbackModel.getContent());
        if (myFeedbackModel.getMessage().equals("null")){
            holder.rl_new.setVisibility(View.GONE);
            holder.tv_text2.setVisibility(View.VISIBLE);
            holder.tv_text2.setText("暂无回复");
        }else {
            holder.rl_new.setVisibility(View.VISIBLE);
            holder.tv_text2.setVisibility(View.GONE);
            if (myFeedbackModel.getCreateTime().equals("null")){
                holder.tv_date1.setText("0");
            }else{
                holder.tv_date1.setText(getUserDate(Long.parseLong(myFeedbackModel.getCreateTime())));
            }
            //0：用户 1：平台
            if (myFeedbackModel.getSource().equals("0")){
                holder.tv_who1.setText("我:");
            }else {
                holder.tv_reply1.setVisibility(View.GONE);
            }
            holder.tv_text1.setText(myFeedbackModel.getMessage());
            if (myFeedbackModel.getSource().equals("0")){
                holder.tv_reply1.setText("暂无回复");
            }else {
                holder.tv_reply1.setVisibility(View.GONE);
            }
        }
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener()
            {
                @Override
                public boolean onLongClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public static String getUserDate(long sformat) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = formatter.format(new Date(sformat));   // 时间戳转换成时间     (这里也是毫秒时间戳)
        return sd;
    }


    @Override
    public int getItemCount() {
        return mydata == null ? 0 : mydata.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_date, tv_reply, tv_who, tv_text,tv_date1,tv_reply1,tv_who1,tv_text1,tv_text2;
        RelativeLayout rl_new;
        public ViewHolder(View itemView) {
            super(itemView);
            //日期
            tv_date = (TextView) itemView.findViewById(R.id.tv_date);
            //暂无回复
            tv_reply = (TextView) itemView.findViewById(R.id.tv_reply);
            //谁说的话
            tv_who = (TextView) itemView.findViewById(R.id.tv_who1);
            //文本
            tv_text = (TextView) itemView.findViewById(R.id.tv_text);
            //新消息日期
            tv_date1 = itemView.findViewById(R.id.tv_date1);
            //标签
            tv_reply1 = itemView.findViewById(R.id.tv_reply1);
            //平台 or 用户
            tv_who1 = itemView.findViewById(R.id.tv_who1);
            //新消息
            tv_text1 = itemView.findViewById(R.id.tv_text1);
            //新消息布局
            rl_new = itemView.findViewById(R.id.rl_new);
            //
            tv_text2 = itemView.findViewById(R.id.tv_text2);
        }
    }
}
